package com.weijian.controller.admin;

import com.weijian.common.pojo.CommonResult;
import com.weijian.dal.vo.ConversationVO;
import com.weijian.service.ChatService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.PermitAll;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/conversation")
public class ChatController {

    final ChatService chatService;
    
    @GetMapping("/chat/create")
    @Operation(summary = "创建对话")
    @PermitAll
    public CommonResult<ConversationVO> create() {
        return CommonResult.success(chatService.create());
    }

    @PutMapping("/chat/edit")
    @Operation(summary = "修改对话")
    @PermitAll
    public CommonResult<Boolean> edit(
            @RequestParam(value = "conversationId") String conversationId,
            @RequestParam(value = "title") String title) {
        chatService.edit(conversationId, title);
        return CommonResult.success(true);
    }

    //    @PostMapping(value="/chat")
    @PostMapping(value="/chat",produces = "text/event-stream;charset=UTF-8")
    @Operation(summary = "通用对话")
    @PermitAll
    public Flux<String> chat(
            @RequestParam(value = "conversationId") String conversationId,
            @RequestParam(value = "message") String message,
            @RequestParam(value = "files", required = false) List<MultipartFile> files,
            @RequestParam(value = "tagNames", required = false) String tagNames) {

        return chatService.chat(conversationId, message, files,tagNames);
    }

    @PostMapping(value="/deepChat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "深度对话")
    @PermitAll
    public Flux<String> deepChat(
            @RequestParam(value = "conversationId") String conversationId,
            @RequestParam(value = "message") String message,
            @RequestParam(value = "files", required = false) List<MultipartFile> files,
            @RequestParam(value = "tagNames", required = false) String tagNames) {
        return chatService.deepChat(conversationId, message, files, tagNames);
    }

    @GetMapping(value="/search")
    @Operation(summary = "混合检索")
    @PermitAll
    public CommonResult<String> search(
            @RequestParam(value = "message") String message,
            @RequestParam(value = "tagNames", required = false) String tagNames) {
        return chatService.search(message, tagNames);
    }

    @DeleteMapping("/chat/del")
    @Operation(summary = "删除对话")
    @PermitAll
    public CommonResult<Boolean> del(@RequestParam(value = "conversationId") String conversationId) {
        chatService.delete(conversationId);
        return CommonResult.success(true);
    }

    @GetMapping("/chat/list")
    @Operation(summary = "展示对话")
    @PermitAll
    public CommonResult<List<ConversationVO>> list() {
        return CommonResult.success(chatService.list());
    }

    @GetMapping("/chat/get")
    @Operation(summary = "获取对话")
    @PermitAll
    public CommonResult<ConversationVO> get(@RequestParam(value = "conversationId") String conversationId) {
        return CommonResult.success(chatService.get(conversationId));
    }


}
